package com.sunxboy.platform.log4j;

import org.apache.log4j.Appender;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.ErrorHandler;
import org.apache.log4j.spi.Filter;
import org.apache.log4j.spi.LoggingEvent;

/**
 * This class allows to return preinstantiated appender to log4j E.g. usefull if configuring through config file, where
 * one does not have control over how appenders are created This class is split into two
 */
public class SpringAppender implements Appender {
    /*
     * This and below is for log4j
     */

    public SpringAppender() {
    }

    private Appender delegate;

    public void addFilter(Filter newFilter) {
        delegate.addFilter(newFilter);
    }

    public void clearFilters() {
        delegate.clearFilters();
    }

    public void close() {
        delegate.close();
    }

    public void doAppend(LoggingEvent event) {
        delegate.doAppend(event);
    }

    public ErrorHandler getErrorHandler() {
        return delegate.getErrorHandler();
    }

    public Filter getFilter() {
        return delegate.getFilter();
    }

    public Layout getLayout() {
        return delegate.getLayout();
    }

    public String getName() {
        return delegate.getName();
    }

    public boolean requiresLayout() {
        return delegate.requiresLayout();
    }

    public void setErrorHandler(ErrorHandler errorHandler) {
        delegate.setErrorHandler(errorHandler);
    }

    public void setLayout(Layout layout) {
        delegate.setLayout(layout);
    }

    public void setName(String name) {
        /*
         * Log4J will call this first after creating this appender
         */
        if (delegate == null) {
            delegate = SpringAppenderConfig.getAppender(name);

            if (delegate == null) {
                throw new RuntimeException("Spring appender not defined: " + name);
            }
        }

        delegate.setName(name);
    }

}
